package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.utils.EnumUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.api.model.query.AgentMemberQuery;
import com.weixing.mall.provider.api.model.vo.AgentMemberAddressVo;
import com.weixing.mall.provider.api.model.vo.DeliveryAddressVo;
import com.weixing.mall.provider.mapper.DeliveryAddressMapper;
import com.weixing.mall.provider.model.domain.DeliveryAddress;
import com.weixing.mall.provider.model.dto.DeliveryAddressDto;
import com.weixing.mall.provider.model.dto.DeliveryAddressPCDto;
import com.weixing.mall.provider.model.query.DeliveryAddressPCQuery;
import com.weixing.mall.provider.model.vo.DeliverAddressPCVo;
import com.weixing.mall.provider.model.vo.DeliveryAddressEditPCVo;
import com.weixing.mall.provider.service.IDeliveryAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员模块-收货地址表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-13
 */
@Service
public class DeliveryAddressServiceImpl extends ServiceImpl<DeliveryAddressMapper, DeliveryAddress> implements IDeliveryAddressService {
    @Override
    public DeliveryAddressEditPCVo selectDetailPC(long id) {
        DeliveryAddress deliveryAddress = getById(id);
        DeliveryAddressEditPCVo deliveryAddressEditPCVo = new DeliveryAddressEditPCVo();
        BeanUtils.copyProperties(deliveryAddress, deliveryAddressEditPCVo);
        ArrayList<String> list = new ArrayList<>();
        String[] split = new String[]{deliveryAddress.getProvince(), deliveryAddress.getCity(), deliveryAddress.getRegion(), deliveryAddress.getTown()};
        for (String s : split) {
            list.add(s);
        }
        deliveryAddressEditPCVo.setProvincesPath(list);
        return deliveryAddressEditPCVo;
    }

    @Override
    public IPage<AgentMemberAddressVo> selectAgentMemberAddressOfPage(Page page, AgentMemberQuery agentMemberQuery) {
        return baseMapper.selectAgentMemberAddressOfPage(page, agentMemberQuery);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(DeliveryAddressDto deliveryAddressDto, Long mid) {
        DeliveryAddress deliveryAddress = null;
        // 其他地址都置为非默认
        if (WhetherEnum.YES.equals(deliveryAddressDto.getIsDefault())) {
            baseMapper.updateIsDefault(mid, WhetherEnum.NO.getValue());
        }
        if (null == deliveryAddressDto.getId()) {
            deliveryAddress = new DeliveryAddress();
            BeanUtils.copyProperties(deliveryAddressDto, deliveryAddress);
            deliveryAddress.setMemberId(mid);
            deliveryAddress.setIsDefault(deliveryAddressDto.getIsDefault().getValue());
            save(deliveryAddress);
        } else {
            deliveryAddress = getById(deliveryAddressDto.getId());
            BeanUtils.copyProperties(deliveryAddressDto, deliveryAddress);
            deliveryAddress.setIsDefault(deliveryAddressDto.getIsDefault().getValue());
            updateById(deliveryAddress);
        }

        return deliveryAddress.getId();
    }

    @Override
    public DeliveryAddressVo selectDetail(Long id) {
        DeliveryAddress deliveryAddress = getById(id);
        DeliveryAddressVo deliveryAddressEditVo = new DeliveryAddressVo();
        BeanUtils.copyProperties(deliveryAddress, deliveryAddressEditVo);
        deliveryAddressEditVo.setIsDefault(EnumUtil.getIEnum(WhetherEnum.class,deliveryAddress.getIsDefault()));
        return deliveryAddressEditVo;
    }

    @Override
    public IPage<DeliverAddressPCVo> selectByMemberIdOfPage(Page page, Long memberId) {
        return baseMapper.selectByMemberIdOfPage(page, memberId);
    }


    @Override
    public List<DeliveryAddressVo> selectMemberAddrList(Long mid) {
        return baseMapper.selectByMemberIdList(mid);
    }

    @Override
    public Long savePC(DeliveryAddressPCDto deliveryAddressPCDto) {
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        BeanUtils.copyProperties(deliveryAddressPCDto, deliveryAddress);
        deliveryAddress.setProvince(deliveryAddressPCDto.getProvincesPath()[0]);
        deliveryAddress.setCity(deliveryAddressPCDto.getProvincesPath()[1]);
        deliveryAddress.setRegion(deliveryAddressPCDto.getProvincesPath()[2]);
        deliveryAddress.setTown(deliveryAddressPCDto.getProvincesPath()[3]);
        if (deliveryAddressPCDto.getId() == null) {
            deliveryAddress.setCreatedTime(new Date());
            save(deliveryAddress);
        } else {
            updateById(deliveryAddress);
        }
        return deliveryAddress.getId();
    }

    @Override
    public IPage<DeliverAddressPCVo> selectListPC(Page page, DeliveryAddressPCQuery deliveryAddressPCQuery) {
        return baseMapper.selectListPC(page, deliveryAddressPCQuery);
    }
}
